<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

//PHP
class class_fieldtype_php_dynamic_output{	

	function display_dynamic_field($row, $params, $dynamic_field_params){
		//the id of the vlaue field is parsed from the dynamic_field tag: return '{pi_dynamic_field php '.$field->value_id.'}';	
		$value_id = $dynamic_field_params;
		//get database
		$database = $this->get_database();
		//get the value
		$database->setQuery("SELECT field_id, item_id, value FROM #__pi_custom_fields_values WHERE id='$value_id' LIMIT 1");
		$values = $database->loadObjectList();
		$value = $values[0];		
		//parse value-id along so we can use this in the php code 
		$php_to_render = '<?php $value_id = \''.$value_id.'\'; 
			$item_id = \''.$value->item_id.'\'; 
			$field_id = \''.$value->field_id.'\';			
			?>';
		$php_to_render .= $value->value;
		//get the output of the processed php
		$html = $this->phpWrapper($php_to_render);
		return $html;
	}
	
	function phpWrapper($content){
		$database = $this->get_database();		
		ob_start();
		eval("?>" . $content);
		$content = ob_get_contents();
		ob_end_clean(); 		
		return $content;
	}
	
	function get_database(){
		if(defined('_JEXEC')){
			//joomla 1.5
			$database = JFactory::getDBO();
		}else{
			//joomla 1.0.x
			global $database;
		}
		return $database;
	}
}

?>